<style>
  .ivu-table .first_node td {
    /* background-color: #2db7f5; */
    color: black;
  }

  .ivu-table .children_node td {
    /* background-color: #e2e8ea; */
    /* color: #2db7f5; */
  }
</style>
<template>
  <div>
    <div class="ivu-mt ivu-card ivu-card-dis-hover">
      <Card>
        <Row :gutter="24">
          <Col :xs="8" :lg="16">
            <Button type="success" icon="plus" @click="addBtn()">{{ $t('add') }}</Button>
          </Col>
        </Row>
        <br>
        <Row>
          <div class="demo-spin-container" v-if="tableLoading">
            <Spin fix>
              <Icon type="load-c" size=18 class="spin-icon-load"></Icon>
              <div>{{ $t('table_loading') }}</div>
            </Spin>
          </div>

          <Table class="no-border-table" editable size="small" :row-class-name="rowClassName" :columns="columns"
                 :data="dataList" @on-sort-change='onSortChange'>

            <template slot-scope="{ row, index }" slot="name">
              <span v-html="row.name"></span>
            </template>
            <template slot-scope="{ row, index }" slot="weight">
              <table-edit table="article_categories" column="weight" :id="row.id" :value="row.weight"
                          :index="index"></table-edit>
            </template>
            <template slot-scope="{ row, index }" slot="enable">
              <iSwitch type='primary' :value="row.enable === 'T'"
                       @on-change="switchChange(row,index,'enable')"></iSwitch>
            </template>
            <template slot-scope="{ row, index }" slot="action">
              <a class="color-primary margin-right-5" type="text"
                 @click="tableButtonEdit(row,index)">{{$t('edit')}} </a>
              <Divider type="vertical"/>
              <a class="color-warning margin-right-5" type="text"
                 @click="tableButtonDestroyOk(row,index)">{{$t('destroy')}} </a>
            </template>
          </Table>
        </Row>
      </Card>
    </div>

    <add-component v-if='addModal.show' :tableStatus_enable="tableStatus.enable"
                   @on-add-modal-success='getTableDataExcute' @on-add-modal-hide="addModalHide"></add-component>
    <edit-component v-if='editModal.show' :tableStatus_enable="tableStatus.enable" :modal-id='editModal.id'
                    @on-edit-modal-success='getTableDataExcute' @on-edit-modal-hide="editModalHide"></edit-component>

  </div>
</template>

<script>
import AddComponent from './components/add'
import EditComponent from './components/edit'
import TableEdit from '_c/common/table-edit.vue'
import { Modal } from 'view-design'

import {
  getTableData,
  destroy
} from '@/api/article_category'
import {
  getTableStatus,
  switchEnable
} from '@/api/common'
import {
  oneOf
} from '@/libs/tools'

export default {
  components: {
    TableEdit,
    AddComponent,
    EditComponent
  },
  data () {
    return {
      searchForm: {
        order_by: 'weight,asc'
      },
      notRealySortKey: [],
      tableStatus: {
        enable: []
      },
      tableLoading: true,
      dataList: [],
      addModal: {
        show: false
      },
      editModal: {
        show: false,
        id: 0
      },
      columns: [{
        title: 'ID',
        key: 'id',
        sortable: 'customer',
        minWidth: 100
      }, {
        title: '分类名称',
        minWidth: 150,
        slot: 'name'
      }, {
        title: 'pid',
        key: 'pid',
        minWidth: 100
      }, {
        title: '分类描述',
        key: 'description',
        minWidth: 150
      }, {
        title: '排序',
        key: 'weight',
        minWidth: 100,
        sortable: 'customer',
        slot: 'weight'
      }, {
        title: '状态',
        minWidth: 150,
        slot: 'enable'
      }, {
        title: '创建时间',
        key: 'created_at',
        minWidth: 150
      }, {
        title: '更新时间',
        key: 'created_at',
        minWidth: 150
      }, {
        title: '操作',
        minWidth: 100,
        slot: 'action'
      }]
    }
  },
  created () {
    let t = this
    t.getTableStatusExcute('article_categories/enable')
  },
  methods: {
    handleReset (name) {
      this.$refs[name].resetFields()
    },
    getTableStatusExcute (params) {
      let t = this
      getTableStatus(params).then(res => {
        t.tableStatus.enable = res.data
        t.getTableDataExcute()
      })
    },
    getTableDataExcute () {
      let t = this
      t.loading = true
      getTableData(t.searchForm).then(res => {
        const response_data = res.data
        t.dataList = response_data
        t.tableLoading = false
      }, function (error) {
        t.tableLoading = false
      })
    },
    tableButtonEdit (row, index) {
      this.editModal.show = true
      this.editModal.id = row.id
    },
    tableButtonDestroyOk (row, index) {
      let t = this
      Modal.confirm({
        title: '确定要删除id为 ' + row.id + ' 的数据吗?',
        onOk: () => {
          destroy(row.id).then(res => {
            t.dataList.splice(index, 1)
      t.$Message.info({
              content: res.message
            })
          })
        },
        onCancel: () => {
        }
      })
    },
    onSortChange: function (data) {
      const order = data.column.key + ',' + data.order
      if (oneOf(data.column.key, this.notRealySortKey)) {

      } else {
        this.searchForm.order_by = order
        this.getTableDataExcute()
      }
    },
    addBtn () {
      this.addModal.show = true
    },
    addModalHide () {
      this.addModal.show = false
    },
    editModalHide () {
      this.editModal.show = false
    },
    rowClassName (row, index) {
      if (row.pid < 1) {
        return 'first_node'
      } else {
        return 'children_node'
      }
    },
    switchChange: function (row, index, column) {
      let t = this
      let new_status = 'T'
      if (t.dataList[index][column] === 'T') {
        new_status = 'F'
      }
      switchEnable(t.dataList[index].id, 'article_categories_column_' + column, new_status).then(res => {
        t.dataList[index][column] = new_status
        t.$Notice.success({
          title: res.message
        })
      }).catch((err) => {
        t.getTableDataExcute()
      })
    }
  }
}
</script>
